package com.example.controller;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestControllerException {

	//单个controller进行异常处理
    @RequestMapping("/testError")
    public String testError() {
        int a = 10 / 0;
        return "this is testError" + a;
    }

    /**
     * 处理其他异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String exceptionHandler(Exception e) {
        System.out.println(e);
        return "this is a controller exception method!";
    }
    
    /**
     * 处理空指针异常
     * @param e
     * @return
     */
    @ExceptionHandler(value =NullPointerException.class)
    public String  exceptionHandler(NullPointerException e){
        System.out.println("发生空指针异常！原因是:"+e);
        return "null";
    }
}
